package SplitHelper

import (
	"gitee.com/tym_hmm/go-helper/RandomHelper"
	"sort"
	"time"
)

// 总数拆分
//入参一个总数，可根据指定副参来拆分

/*
 * 将一个总数(sum) 拆分成n组 最小数为m
 * 随机分配 每次个数和为总数
 * ex:
 * sum: 20(总合) , n:2(两组), m:1(最小数1)
 * out:
 * random out: [18 2]
 * random out: [19 1]
 * random out: [17 3]
 * random out: [8 12]
 * @param n int 拆分成数量
 * @param sum int 总和
 * @param m int 最小数
 */
func splitSumRandGroup(n int, sum int, m int) []int {
	//随机抽取n-1个小于sum的数
	var list []int = []int{}
	//将0和sum加入到里list中
	list = append(list, 0)
	//每个数至少为m
	sum = sum - n*m
	list = append(list, sum)
	//生成n-1个小于sum的随机数
	var temp int = 0
	for i := 0; i < n-1; i++ {
		temp = int(RandomHelper.RandomAroundFloat64Seed(time.Now().UnixNano()+int64(i), 0, 1) * float64(sum))
		list = append(list, temp)
	}
	sort.Ints(list)
	var nums = []int{}
	for i := 0; i < n; i++ {
		//每份最少为m
		ms := list[i+1] - list[i] + m
		nums = append(nums, ms)
	}
	return nums
}

/**
 * 将一个数生成连接的数并分组
 * @param total 总数
 * @param groupSize 每组大小
 * [1 2 3 4 5]
 * [6 7 8 9 10]
 * [11 12 13 14 15]
 * [16 17 18 19 20]
 */
func SplitSumContinuousGroup(total int, groupSize int) [][]int {
	groups := make([][]int, total/groupSize+1) // 创建切片存放结果
	for i := range groups {
		start := i * groupSize
		end := start + groupSize - 1
		if end >= total {
			end = total - 1
		}
		groups[i] = makeRange(start+1, end+1) // 生成每个分组的范围
	}
	return groups
}

// 生成指定范围内的连续数字序列
func makeRange(start, end int) []int {
	rangeSlice := make([]int, end-start+1)
	for i := range rangeSlice {
		rangeSlice[i] = start + i
	}
	return rangeSlice
}
